import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import { isLogin } from '../utils/auth'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    meta: {
      isHome: true
    },
    component: Home
  },
  {
    path: '/hot',
    name: 'Hot',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Hot.vue')
  },
  {
    path: '/user',
    name: 'User',
    meta: {
      needLogin: true
    },
    component: () => import('../views/User.vue')
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      isShowFooter: true
    },
    component: () => import('../views/Login.vue')
  },
  {
    path: '/reg',
    name: 'Reg',
    meta: {
      isShowFooter: true
    },
    component: () => import('../views/Reg.vue')
  },
  {
    path: '/cart',
    name: 'Cart',
    meta: {
      needLogin: true
    },
    component: () => import('../views/Cart.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  /* console.group('before')
  console.log(to)
  console.log(from)
  console.log(next)
  next()
  console.groupEnd() */
  if (to.meta.needLogin) {
    if (isLogin()) {
      next()
    } else {
      next({ name: 'Login' })
    }
  } else {
    next()
  }
})
router.afterEach((to, from) => {
  /* console.group('after')
  console.log(to)
  console.log(from)
  console.groupEnd() */
})
export default router
